import {Component, OnInit} from '@angular/core';
import {NgbTabChangeEvent} from '@ng-bootstrap/ng-bootstrap';

import {ArticleService} from "./service/article.service";
import {Article} from "./entity/article";
import {ArticleArea} from "./entity/articleArea";


@Component({
    moduleId: module.id,
    selector: 'my-article',
    templateUrl: 'article.component.html',
    styleUrls: ['article.component.css'],
    providers: [ArticleService]
})

export class ArticleComponent implements OnInit{

    constructor(private articleService: ArticleService) {
    }

    errorMessage: string;
    articles: Article[];
    recommendeds: Article[];
    hottests: Article[];
    latests: Article[];
    articlesAreas: ArticleArea[];
    page = 1;
    aaid = "1";

    ngOnInit(): void {
        this.articleService.getArticlesAreas().subscribe(articlesAreas => this.articlesAreas = articlesAreas, error => this.errorMessage = <any>error);
        this.articleService.getArticles(1,"1").subscribe(articles => {
                this.articles = articles;
            }
            , error => this.errorMessage = <any>error);
        this.articleService.getArticles(1,"1").subscribe(articles => {
                this.recommendeds = articles;
            }
            , error => this.errorMessage = <any>error);
        this.articleService.getArticles(1,"1").subscribe(articles => {
                this.hottests = articles;
            }
            , error => this.errorMessage = <any>error);
        this.articleService.getArticles(1,"1").subscribe(articles => {
                this.latests = articles;
            }
            , error => this.errorMessage = <any>error);
    }

    beforeChange($event: NgbTabChangeEvent) {
/*        if ($event.nextId === 'comprehensive') {

            console.log("comprehensive");
        }*/
        this.aaid = $event.nextId;
        this.page = 1;
        this.articleService.getArticles(this.page,this.aaid).subscribe(articles => {
                this.articles = articles;
            }
            , error => this.errorMessage = <any>error);
    };

    pageChange(page: number):void {
        this.articleService.getArticles(page,this.aaid).subscribe(articles => {
                this.articles = articles;
            }
            , error => this.errorMessage = <any>error);
        this.articleService.getArticles(page,this.aaid).subscribe(articles => {
                this.recommendeds = articles;
            }
            , error => this.errorMessage = <any>error);
        this.articleService.getArticles(page,this.aaid).subscribe(articles => {
                this.hottests = articles;
            }
            , error => this.errorMessage = <any>error);
        this.articleService.getArticles(page,this.aaid).subscribe(articles => {
                this.latests = articles;
            }
            , error => this.errorMessage = <any>error);

            console.log(page);
        }

}
